package com.meituan.catering.management.order.biz.model.converter;

import com.meituan.catering.management.common.model.api.http.AuditingHttpModel;
import com.meituan.catering.management.common.model.biz.AuditingBO;
import com.meituan.catering.management.order.api.http.model.dto.CateringOrderDetailHttpDTO;
import com.meituan.catering.management.order.api.http.model.enumeration.CateringOrderPaymentChannelEnum;
import com.meituan.catering.management.order.biz.model.CateringOrderDetailBO;
import com.meituan.catering.management.order.biz.model.CateringOrderItemAccessoryBO;
import com.meituan.catering.management.order.biz.model.CateringOrderItemBO;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Believe
 * Date 2022/5/28 14:52
 */
public class CateringOrderDetailDTOConverter {
    public static CateringOrderDetailHttpDTO toCateringOrderDetailHttpDTO(CateringOrderDetailBO cateringOrderDetailBO) {
        CateringOrderDetailHttpDTO cateringOrderDetailHttpDTO = new CateringOrderDetailHttpDTO();
        cateringOrderDetailHttpDTO.setId(cateringOrderDetailBO.getCateringOrderBO().getId());
        cateringOrderDetailHttpDTO.setTenantId(cateringOrderDetailBO.getCateringOrderBO().getTenantId());
        cateringOrderDetailHttpDTO.setVersion(cateringOrderDetailBO.getCateringOrderBO().getVersion());
        cateringOrderDetailHttpDTO.setStatus(cateringOrderDetailBO.getCateringOrderBO().getStatus());
        cateringOrderDetailHttpDTO.setTableNo(cateringOrderDetailBO.getCateringOrderBO().getTableNo());
        cateringOrderDetailHttpDTO.setCustomerCount(cateringOrderDetailBO.getCateringOrderBO().getCustomerCount());
        cateringOrderDetailHttpDTO.setTotalPrice(cateringOrderDetailBO.getCateringOrderBO().getTotalPrice());
        cateringOrderDetailHttpDTO.setComment(cateringOrderDetailBO.getCateringOrderBO().getComment());
        cateringOrderDetailHttpDTO.setAuditing(toAuditingHttpModel(cateringOrderDetailBO.getCateringOrderBO().getAuditing()));
        cateringOrderDetailHttpDTO.setShopSnapshotOnPlace(toShopSnapshot(
                cateringOrderDetailBO.getCateringOrderBO().getShopId(),
                cateringOrderDetailBO.getCateringOrderBO().getShopBusinessNo(),
                cateringOrderDetailBO.getCateringOrderBO().getShopNameOnPlace()
        ));
        cateringOrderDetailHttpDTO.setBilling(toBilling(
                cateringOrderDetailBO.getCateringOrderBO().getBillingPromotion(),
                cateringOrderDetailBO.getCateringOrderBO().getBillingPaid(),
                cateringOrderDetailBO.getCateringOrderBO().getBillingPaymentChannel()
        ));
        cateringOrderDetailHttpDTO.setItems(toItem(cateringOrderDetailBO.getCateringOrderItemBOList(), cateringOrderDetailBO.getCateringOrderItemAccessoryBOList()));

        return cateringOrderDetailHttpDTO;
    }

    public static AuditingHttpModel toAuditingHttpModel(AuditingBO auditingBO){
        return new AuditingHttpModel(
                auditingBO.getCreatedBy(),
                auditingBO.getCreatedAt(),
                auditingBO.getLastModifiedBy(),
                auditingBO.getLastModifiedAt()
        );
    }

    public static CateringOrderDetailHttpDTO.ShopSnapshot toShopSnapshot(Long id, String businessNo, String name) {
        return new CateringOrderDetailHttpDTO.ShopSnapshot(id, businessNo, name);
    }

    public static CateringOrderDetailHttpDTO.Billing toBilling(BigDecimal promotion, BigDecimal paid, CateringOrderPaymentChannelEnum channelEnum) {
        return new CateringOrderDetailHttpDTO.Billing(promotion, paid, channelEnum);
    }

    public static List<CateringOrderDetailHttpDTO.Item> toItem(List<CateringOrderItemBO> orderItemBOList, List<CateringOrderItemAccessoryBO> orderItemAccessoryBOList) {
        List<CateringOrderDetailHttpDTO.Item> itemList = new LinkedList<>();
        for (CateringOrderItemBO cateringOrderItemBO : orderItemBOList) {
                CateringOrderDetailHttpDTO.Item item = new CateringOrderDetailHttpDTO.Item();
                item.setId(cateringOrderItemBO.getId());
                item.setVersion(cateringOrderItemBO.getVersion());
                item.setStatus(cateringOrderItemBO.getStatus());
                item.setSeqNo(cateringOrderItemBO.getSeqNo());
                item.setQuantity(toQuantity(
                        cateringOrderItemBO.getPlaceQuantity(),
                        cateringOrderItemBO.getLatestQuantity(),
                        cateringOrderItemBO.getProduceQuantity()
                ));
                item.setProductSnapshotOnPlace(toProductSnapshot(
                        cateringOrderItemBO.getProductId(),
                        cateringOrderItemBO.getProductNameOnPlace(),
                        cateringOrderItemBO.getProductUnitPriceOnPlace(),
                        cateringOrderItemBO.getProductUnitOfMeasureOnPlace()
                ));
                item.setProductMethodSnapshotOnPlace(toProductMethodSnapshot(
                        cateringOrderItemBO.getProductMethodId(),
                        cateringOrderItemBO.getProductMethodNameOnPlace(),
                        cateringOrderItemBO.getProductMethodGroupNameOnPlace()
                ));
                item.setAccessories(toAccessory(cateringOrderItemBO.getId(),orderItemAccessoryBOList));
                itemList.add(item);
            }
        return itemList;
    }

    public static List<CateringOrderDetailHttpDTO.Item.Accessory> toAccessory(Long id, List<CateringOrderItemAccessoryBO> orderItemAccessoryBOList) {
        List<CateringOrderDetailHttpDTO.Item.Accessory> accessoryList = new LinkedList<>();
        for (CateringOrderItemAccessoryBO cateringOrderItemAccessoryBO : orderItemAccessoryBOList) {
            CateringOrderDetailHttpDTO.Item.Accessory accessory = new CateringOrderDetailHttpDTO.Item.Accessory();
            if (id.equals(cateringOrderItemAccessoryBO.getOrderItemId())){
                accessory.setId(cateringOrderItemAccessoryBO.getId());
                accessory.setVersion(cateringOrderItemAccessoryBO.getVersion());
                accessory.setSeqNo(cateringOrderItemAccessoryBO.getSeqNo());
                accessory.setStatus(cateringOrderItemAccessoryBO.getStatus());
                accessory.setQuantity(toAccessoryQuantity(
                        cateringOrderItemAccessoryBO.getPlaceQuantity(),
                        cateringOrderItemAccessoryBO.getLatestQuantity(),
                        cateringOrderItemAccessoryBO.getProduceQuantity()
                ));
                accessory.setProductAccessorySnapshotOnPlace(toProductAccessorySnapshot(cateringOrderItemAccessoryBO));
                accessoryList.add(accessory);
            }
        }
        return accessoryList;
    }

    public static CateringOrderDetailHttpDTO.Item.Accessory.ProductAccessorySnapshot toProductAccessorySnapshot(CateringOrderItemAccessoryBO cateringOrderItemAccessoryBO){
        return new CateringOrderDetailHttpDTO.Item.Accessory.ProductAccessorySnapshot(
                cateringOrderItemAccessoryBO.getId(),
                cateringOrderItemAccessoryBO.getProductAccessoryNameOnPlace(),
                cateringOrderItemAccessoryBO.getProductAccessoryGroupNameOnPlace(),
                cateringOrderItemAccessoryBO.getProductAccessoryUnitPriceOnPlace(),
                cateringOrderItemAccessoryBO.getProductAccessoryUnitOfMeasureOnPlace()
        );
    }

    public static CateringOrderDetailHttpDTO.Item.Accessory.Quantity toAccessoryQuantity(BigDecimal onPlace, BigDecimal latest, BigDecimal onProduce) {
        return new CateringOrderDetailHttpDTO.Item.Accessory.Quantity(latest, onPlace, onProduce);
    }

    public static CateringOrderDetailHttpDTO.Item.ProductMethodSnapshot toProductMethodSnapshot(Long id, String name, String groupName) {
        return new CateringOrderDetailHttpDTO.Item.ProductMethodSnapshot(id, name, groupName);
    }

    public static CateringOrderDetailHttpDTO.Item.ProductSnapshot toProductSnapshot(Long id, String name, BigDecimal unitPrice, String unitOfMeasure) {
        return new CateringOrderDetailHttpDTO.Item.ProductSnapshot(id, name, unitPrice, unitOfMeasure);
    }

    public static CateringOrderDetailHttpDTO.Item.Quantity toQuantity(BigDecimal onPlace, BigDecimal latest, BigDecimal onProduce) {
        return new CateringOrderDetailHttpDTO.Item.Quantity(onPlace, latest, onProduce);
    }
}
